package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page findByPage(Integer pageNum, Integer pageSize, String name) {
        //1-1.设置分页条件
        Page<Dish> page = new Page<Dish>(pageNum,pageSize);
        //1-2.设置业务条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //1-3.执行查询
        page=dishMapper.selectPage(page,wrapper);
        //2.遍历集合
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3.获取分类名并存储
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
//                //4.获取口味列表并存储
//                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
//                wrapper1.eq(DishFlavor::getDishId,dish.getId());
//                dish.setFlavors(dishFlavorMapper.selectList(wrapper1));
            }
        }
        System.out.println(page);
        return page;
    }
    //新增菜品
    @Override
    public void save(Dish dish) {
        //1.将菜品信息保存到菜品表
        dishMapper.insert(dish);    //它会自动返回主键id
        //2.接收口味列表，遍历
        if (CollectionUtil.isNotEmpty(dish.getFlavors())) {
            for (DishFlavor flavor : dish.getFlavors()) {
                //为每个口味添加dish_id
                flavor.setDishId(dish.getId());
                //保存到dish_flavor表中
                dishFlavorMapper.insert(flavor);
            }
        }
    }
    //根据id查询
    @Override
    public Dish findById(Long id) {
        //1.根据id查询菜品表
        Dish dish = dishMapper.selectById(id);
        //2.获取分类名并存储
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //3.获取口味列表并存储
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        dish.setFlavors(dishFlavorMapper.selectList(wrapper1));
        return dish;
    }

    @Override
    public void update(Dish dish) {
        //1.调用mapper修改dish表
        dishMapper.updateById(dish);
        //2.删除跟本菜品相关的口味表的信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        //3.重新遍历口味信息并添加到口味表中
        if (CollectionUtil.isNotEmpty(dish.getFlavors())) {
            for (DishFlavor flavor : dish.getFlavors()) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }
    //删除菜品
    @Override
    public void delete(List<Long> ids) {
        //1.根据id查询dish，有起售状态的就直接抛异常
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("包含起售状态的菜品，无法删除");
        }
        //2.遍历ids
        if (CollectionUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                //先删除该菜品相关的口味信息
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, id);
                dishFlavorMapper.deleteById(id);
                //再删除该菜品信息
                dishMapper.deleteById(id);
            }
        }
    }
    //修改售卖状态
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Dish dish = findById(id);
                dish.setStatus(status);
                dishMapper.updateById(dish);
            }
        }
    }
    //根据category_Id查询
//    @Override
//    public List<Dish> findList(Long categoryId) {
//        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Dish::getStatus,1).eq(Dish::getCategoryId,categoryId);
//        List<Dish> dishList = dishMapper.selectList(wrapper);
//        return dishList;
//    }
    //查询菜品列表
    @Override
    public List<Dish> findList(Long categoryId,String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus,1)   //在售的
                .eq(categoryId!=null,Dish::getCategoryId,categoryId)    //匹配分类id
                .like(StringUtils.isNotEmpty(name),Dish::getName,name);   //匹配名字
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //---------------------------遍历菜品集合，查询每个菜品口味-----------------------------
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }
        return dishList;
    }
}
